package com.ghn.cocknovel.ui.activity.Information

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.RadioButton
import androidx.annotation.RequiresApi
import androidx.core.view.get
import com.example.basemodel.base.BaseHeadVMActivity
import com.example.basemodel.base.DefaultHeadStyle
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.ActivityInformationBinding
import com.ghn.cocknovel.ui.activity.Information.vm.InformationViewMldel
import com.ghn.cocknovel.ui.activity.address.SelecAddressActivity
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout

class InformationActivity :
    BaseHeadVMActivity<InformationViewMldel, ActivityInformationBinding, DefaultHeadStyle>() {
    val RESULT_ADDRESS = 100
    var mProvinceName: String? = null
    var mProvinceId: String? = null
    var mcityName: String? = null
    var mCityId: String? = null
    var mCountyName: String? = null
    var mCountyId: String? = null
    var driverGender:Int?=null
    override fun layoutId() = R.layout.activity_information

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView(savedInstanceState: Bundle?) {
        mHeadStyle.setTitleColor("基本信息", getColor(R.color.black))
        mHeadStyle.setTitleBackgroundColor(getColor(R.color.gray4))
        mDatabind.btnSeletctAddress.setOnClickListener {
            startActivityForResult(Intent(this, SelecAddressActivity::class.java), RESULT_ADDRESS)
        }
        mDatabind.btnStartTimet.setOnClickListener {
            var timeSplit = emptyArray<String>()
            if (mDatabind.btnStartTimet.text.toString().isNotEmpty()) {
                timeSplit = mDatabind.btnStartTimet.text.split("-").toTypedArray()
            }
            val picker = DatePicker(this)
            picker.setOnDatePickedListener { year, month, day ->
                mDatabind.btnStartTimet.text = "${year}-${month}-${day}"
            }
            picker.setBodyWidth(240)
            val wheelLayout: DateWheelLayout = picker.wheelLayout
            wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
            wheelLayout.setDateLabel("年", "月", "日")
            if (timeSplit.isNotEmpty()) {
                wheelLayout.setRange(
                    DateEntity.target(2000, 1, 1),
                    DateEntity.today(),
                    DateEntity.target(
                        timeSplit[0].toInt(),
                        timeSplit[1].toInt(),
                        timeSplit[2].toInt()
                    )
                )
            } else {
                wheelLayout.setRange(DateEntity.target(2023, 1, 1), DateEntity.today())
            }
            wheelLayout.setIndicatorEnabled(true)
            picker.show()
        }
        mDatabind.btnEndTimetAddress.setOnClickListener {
            var timeSplit = emptyArray<String>()
            if (mDatabind.btnEndTimetAddress.text.toString().isNotEmpty()) {
                timeSplit = mDatabind.btnEndTimetAddress.text.split("-").toTypedArray()
            }
            val picker = DatePicker(this)
            picker.setOnDatePickedListener { year, month, day ->
                mDatabind.btnEndTimetAddress.text = "${year}-${month}-${day}"
            }
            picker.setBodyWidth(240)
            val wheelLayout: DateWheelLayout = picker.wheelLayout
            wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
            wheelLayout.setDateLabel("年", "月", "日")
            if (timeSplit.isNotEmpty()) {
                wheelLayout.setRange(
                    DateEntity.target(2000, 1, 1),
                    DateEntity.today(),
                    DateEntity.target(
                        timeSplit[0].toInt(),
                        timeSplit[1].toInt(),
                        timeSplit[2].toInt()
                    )
                )
            } else {
                wheelLayout.setRange(DateEntity.target(2023, 1, 1), DateEntity.today())
            }
            wheelLayout.setIndicatorEnabled(true)
            picker.show()
        }
        mDatabind.radioGroup.setOnCheckedChangeListener { radioGroup, i ->
            when(findViewById<RadioButton>(i).text){
                "男"->{
                    driverGender=1
                }
                "女"->{
                    driverGender=2
                }
            }
        }
        mDatabind.informationcompanysubmit.setOnClickListener {
            mViewModel.message.observe(this) {
                mViewModel.driverUpdateTransitDriverInfo(
                    mDatabind.informationedname.text.toString(),
                    it?.driverAvatar.toString(),
                    driverGender!!,
                    mDatabind.btnEndTimetAddress.text.toString(),
                    mDatabind.informationedloginname.text.toString(),
                    mDatabind.informationedjobnumber.text.toString(),
                    mDatabind.informationcompanyname.text.toString(),
                    mDatabind.btnStartTimet.text.toString(),
                    (mDatabind.informationeddrivingyears.text.toString()).toInt(),
                    mProvinceId.toString(),
                    mProvinceName.toString(),
                    mCityId.toString(),
                    mcityName.toString(),
                    mCountyId.toString(),
                    mCountyName.toString()
                )
                mViewModel.UpdateTransitDriverInfo.observe(this){
                    if (it?.code==1){
                        showMsg(it.desc.toString())
                    }
                }
            }
        }

    }

    override fun createObserver() {
        mViewModel.getmessage()
        mViewModel.message.observe(this) {
            mDatabind.informationedname.setText(it?.driverName)
            mDatabind.informationedloginname.setText(it?.accountName)
            mDatabind.informationedjobnumber.setText(it?.driverNumber)
            mDatabind.informationeddrivingyears.setText(it?.drivingAge.toString())
            mDatabind.informationcompanyname.setText(it?.organizationName)
            mDatabind.btnStartTimet.text = it?.entryTime
            mDatabind.btnEndTimetAddress.text = it?.driverBirth
            mDatabind.btnSeletctAddress.text = it?.driverAddress

            when(it?.driverGender){
                1->{
                    mDatabind.btnMan.isChecked=true
                }
                2->{
                    mDatabind.btnWoman.isChecked=true
                }
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                RESULT_ADDRESS -> {
                    data?.also {
                        mProvinceName = it.getStringExtra("provinceName").toString()
                        mProvinceId = it.getStringExtra("provinceId").toString()
                        mcityName = it.getStringExtra("cityName").toString()
                        mCityId = it.getStringExtra("cityId").toString()
                        mCountyName = it.getStringExtra("countyName").toString()
                        mCountyId = it.getStringExtra("countyId").toString()
//                        it.getStringExtra("streetName")
                        mDatabind.btnSeletctAddress.setText(it.getStringExtra("address"))
                    }
                }
            }
        }
    }


}